<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateGoodsTable extends Migration
{
    /**
     * 数据表名称
     * @var string
     */
    private $tableName = 'goods';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        // 创建数据表
        Schema::create($this->tableName, function (Blueprint $table) {
            // 数据表类型
            $table->engine = 'InnoDB';
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';

            // 数据表字段
            $table->bigIncrements('goods_id')->comment('递增主键');
            $table->bigInteger('user_id', false, true)->comment('发布用户');
            $table->string('goods_code', 50)->comment('商品编码');
            $table->decimal('label_price', 10, 2, true)->comment('起拍价');
            $table->string('goods_name', 100)->nullable()->comment('商品名称');
            $table->decimal('cost_price', 10, 2, true)->default(0)->comment('成本价');
            $table->decimal('weight', 10, 2, true)->nullable()->comment('商品重量');
            $table->integer('stock', false, true)->default(1)->comment('商品片数');
            $table->string('seat', 20)->nullable()->comment('商品位置');
            $table->integer('supplier_id', false, true)->nullable()->comment('货主主键');
            $table->string('remarks', 200)->nullable()->comment('商品备注');
            $table->text('description')->nullable()->comment('商品描述');
            $table->tinyInteger('is_bid', false, true)->default(1)->comment('是否参与投标 0:否 1:是');
            $table->tinyInteger('is_onshelf', false, true)->default(1)->comment('是否上架 0:否 1:是');
            $table->tinyInteger('is_show', false, true)->default(0)->comment('是否展示 0:否 1:是');
            $table->dateTime('released_at')->comment('发布上架时间');
            $table->dateTime('deleted_at')->nullable()->comment('数据删除时间');
            $table->dateTime('created_at')->comment('数据创建时间');
            $table->timestamp('updated_at')->comment('最后更新时间')->default(DB::raw('CURRENT_TIMESTAMP'));

            // 数据表索引
            $table->unique('goods_id');
            $table->index('goods_code');
            $table->index('released_at');
        });
        // 设置数据表备注信息
        DB::statement("ALTER TABLE `".env('DB_PREFIX')."{$this->tableName}` COMMENT'标品表'");
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists($this->tableName);
    }
}
